package cn.edu.zut.soft.student.controller.admin.student;

import cn.edu.zut.soft.student.bean.ResultBean;
import cn.edu.zut.soft.student.model.Student;
import cn.edu.zut.soft.student.model.enums.Role;
import cn.edu.zut.soft.student.service.student.StudentBo;
import cn.edu.zut.soft.student.service.student.vo.StudnetQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreatDate 2021/02/22 15:06
 */

@RestController
@RequestMapping("/admin/api/student")
public class StudentController {
    @Autowired
    private StudentBo studentBo;

    @PostMapping("")
    public ResultBean<Integer> addStudent(Student student) {
        student.setPassword(Role.student.getPassword());
        return new ResultBean<>(studentBo.addStudent(student));
    }

    @PostMapping("/import")
    public ResultBean<Integer> importStudents(MultipartFile file) throws Exception {
        return new ResultBean<>(studentBo.importStudents(file));
    }

    @DeleteMapping("/{studentid}")
    public ResultBean<Integer> deleteCourse(@PathVariable("studentid") String studentid) {
        return new ResultBean<>(studentBo.deleteStudent(studentid));
    }

    @DeleteMapping("/list")
    public ResultBean<Integer> deleteStudents(String allStudentIds) {
        String[] ids = allStudentIds.substring(1, allStudentIds.length() - 1).split(",");
        for (String id : ids) {
            studentBo.deleteStudent(id);
        }
        return new ResultBean<>();
    }

    @PutMapping("")
    public ResultBean<Integer> updateStudent(Student student) {
        return new ResultBean<>(studentBo.updateStudent(student));
    }

    @PutMapping("/{studentid}")
    public ResultBean<Integer> resetPassword(@PathVariable("studentid") String studentid) {
        return new ResultBean<>(studentBo.resetPassword(studentid, Role.student.getPassword()));
    }

    @GetMapping("/{studentid}")
    public ResultBean<Student> queryStudent(@PathVariable("studentid") String studentid) {
        return new ResultBean<>(studentBo.queryStudentById(studentid));
    }

    @GetMapping("")
    public ResultBean<List<Student>> queryStudents(StudnetQuery studnetQuery) {
        return new ResultBean<>(studentBo.queryStudents(studnetQuery));
    }
}
